 package com.marvels.financial.loan.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.LoanAuditConditionDto;
import com.marvels.financial.loan.dto.McptProductMappedDto;
import com.marvels.financial.loan.service.LoanQueryApi;



/**
 * 放款查询
 * @author tangpw
 *
 */
@RestController
@RequestMapping("financial/loan/loanQuery/service")
public class LoanQueryController {
	
	
	@Autowired 
	private LoanQueryApi loanQueryApi;
	
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	
	
	/**
	 * 放款查询筛选条件
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("listScreenCondition")
	@ResponseBody
	public MarvelsResultDTO listScreenCondition() {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map = loanQueryApi.listScreenCondition();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					map);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>>>>>>>放款查询list的筛选条件FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>放款查询list的筛选条件异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}

	}
	
	
	/**
	 * 放款查询清单
	 * @param loanAuditConditionDto
	 * @return
	 */
	@RequestMapping("loanQueryList")
	@ResponseBody
	public MarvelsResultDTO loanQueryList(@RequestBody LoanAuditConditionDto loanAuditConditionDto){
		try {
			return loanQueryApi.loanQueryList(loanAuditConditionDto);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>放款查询-放款查询清单查询FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>放款查询-放款查询清单查询异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
	
	
	
	
	/**
	 * excel下载
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("downLoad")
	public void exportExcel(LoanAuditConditionDto loanAuditConditionDto,HttpServletRequest request,HttpServletResponse response){
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		OutputStream outs = null;
		try {
			response.setHeader("Content-disposition", "attachment; filename="+ new String("放款查询一览表.xls".getBytes("UTF-8"),"ISO8859-1"));
			HSSFWorkbook downLoadDeal2 = loanQueryApi.exportExcel(loanAuditConditionDto);
			outs = response.getOutputStream();// 获取文件输出IO流
			downLoadDeal2.write(outs);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载放款申请数据FAIL:", e);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>下载放款申请数据异常", e);
		}finally{
			try {
				outs.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	
	
	
	/**
	 * 线下放款 -- 放款查询列表里面的放款操作
	 * @param loanId
	 * @return
	 */
	@RequestMapping("loanPayment")
	@ResponseBody
	public MarvelsResultDTO loanPayment(@RequestBody String loanId){
		try {
			return loanQueryApi.loanPayment(loanId);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>手动放款FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>手动放款异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
	
	
	/**
	 * 线上放款
	 * @param loanId
	 * @return
	 */
	@RequestMapping("onlineLendPayment")
	@ResponseBody
	public MarvelsResultDTO onlineLendPayment(@RequestBody String loanId){
		try {
			return loanQueryApi.onlineLendPayment(loanId);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>线上放款FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>线上放款异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
	
	
	/**
	 * 校验状态
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("checkStatus")
	@ResponseBody
	public MarvelsResultDTO checkStatus(@RequestBody String loanId){
		try {
			return loanQueryApi.checkStatus(loanId);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>放款手动校验状态FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>放款手动校验状态:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
	
	
	/**
	 * 更换资金源
	 * @param listQueryConditionDto
	 * @return
	 */
	@RequestMapping("replaceFundResource")
	@ResponseBody
	public MarvelsResultDTO replaceFundResource(@RequestBody LoanAuditConditionDto loanAuditConditionDto){
		try {
			loanQueryApi.replaceFundResource(loanAuditConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					null);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>更换资金源操作FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>更换资金源异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}

	}
	
	
	/**
	 * 查询可以更换的资金源列表
	 * @param loanAuditConditionDto
	 * @return
	 */
	@RequestMapping("changeFundList")
	@ResponseBody
	public MarvelsResultDTO changeFundList(@RequestBody LoanAuditConditionDto loanAuditConditionDto){
		try {
			List<McptProductMappedDto> list=loanQueryApi.changeFundList(loanAuditConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					list);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>查询可以更换的资金源列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>查询可以更换的资金源列表异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
	
	/**
	 * 出账否决
	 * @param loanId
	 * @return
	 */
	@RequestMapping("accountVeto")
	@ResponseBody
	public MarvelsResultDTO accountVeto(@RequestBody LoanAuditConditionDto loanAuditConditionDto){
		try {
			loanQueryApi.accountVeto(loanAuditConditionDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),
					null);
		}catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>出账否决操作FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),e.getMessage(), null);
		}catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>出账否决操作异常:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),
					null);
		}
	}
	
}
